package com.example.news;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.example.news.databinding.ActivityMainBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        binding=ActivityMainBinding.inflate(getLayoutInflater());
        super.onCreate(savedInstanceState);
        setContentView(binding.getRoot());

        binding.viewPage.setAdapter(new ViewPagerAdapt(getSupportFragmentManager()));
        ViewPagerAdapt adapter=new ViewPagerAdapt(getSupportFragmentManager());
        binding.viewPage.setAdapter(adapter);
        binding.viewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.navigation.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        binding.navigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
        binding.fabActivityMain.setOnClickListener(v -> Snackbar.make(v,"是否新增一条短信",Snackbar.LENGTH_SHORT).setAction("确定",new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"新增成功",Toast.LENGTH_SHORT).show();
            }
        }).show());
    }

private static class ViewPagerAdapt extends FragmentPagerAdapter{

    public ViewPagerAdapt(@NonNull FragmentManager fm) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return HomeFragment.newInstance();
            case 1:
                return DashboardFragment.newInstance();
            case 2:
                return NotificationsFragment.newInstance();
        }
        return HomeFragment.newInstance();
    }

    @Override
    public int getCount() {
        return 3;
    }
}

    private final BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener=new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            final int itemId=item.getItemId();
            if (itemId==R.id.navigation_home)
            {
                binding.viewPage.setCurrentItem(0);
                return true;
            }
            if (itemId==R.id.navigation_dashboard)
            {
                binding.viewPage.setCurrentItem(1);
                return true;
            }
            if (itemId==R.id.navigation_notifications)
            {
                binding.viewPage.setCurrentItem(2);
            }
            return false;
        }
    };
}